Ext.ns('ru.proburo.oms.order.components.import');

/**
 * ru.proburo.oms.order.components.import.Dialog
 * @extends Ext.Window
 */
ru.proburo.oms.order.components['import'].Dialog = Ext.extend(Ext.Window, {
    
    constructor: function(config)
	{
		config = config || {};
        
        this._form = new Ext.FormPanel({ 
            url:        '/services/order/list.json', 
            fileUpload: true,
            frame:      true, 
            autoHeight: true,
            labelWidth: 38,
            
            
            items:
            [
                {
                    xtype:		'hidden',
                    name: 		'xaction',
                    value:      'import'
                },
                {
                    xtype:		'fileuploadfield',
                    name: 		'file',
                    emptyText:  'Выберите файл..',
                    fieldLabel: 'Файл',
                    buttonCfg:  {
                        text:    'Обзор..',
                        iconCls: 'silk-magnifier'
                    }
                }
            ],
            buttons: [
                {
                    text:     'ОК',
                    handler:  this._doOK,
                    formBind: true,
                    scope:    this
                },
                {
                    text:     'Отмена',
                    handler:  this._doCancel,
                    scope:    this
                }
            ]
        });
        
        Ext.applyIf(config, {
            title:      'Импорт заказа',            
            layout:     'fit',
            width:      315,
            autoHeight: true,
            closable:   false,
            resizable:  false,
            plain:      false,
            border:     false,
            items:      [this._form]
        });
		
        ru.proburo.oms.order.components['import'].Dialog.superclass.constructor.call(this, config);
    },
    
    initEvents: function()
    {
        ru.proburo.oms.order.components['import'].Dialog.superclass.initEvents.call(this);
        
        this.addEvents({
            'complete':  true,
			'exception': true
        });
        
        this._form.getForm().on({
            'actioncomplete': this._onSuccess,
            'actionfailed':   this._onFailure,
            scope: this
        });
    },
    
    
    
    /**
     * Action handlers
     */
    // private
    _doOK: function()
    {
        this._form.getForm().submit();
    },    
    // private
    _doCancel: function()
    {
        this.destroy();
    },
    
    
    
    /**
     * Listeners
     */
    // private
    _onSuccess: function(form, action)
    {
        this.fireEvent('complete', this, action.result.order, action.result.items);
    },
    // private
    _onFailure: function(form, action)
    {
        var exception = new Ext.Error();
        
        if(action.result && action.result.message)
        {
            exception.message = action.result.message;
        }
        
        this.fireEvent('exception', this, exception);
    }
});